package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.BannerNode;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ScreenUtils;
import reduce.fz.com.reduce.util.Utils;
import reduce.fz.com.reduce.util.http.ImageLoader;

/**
 * banner广告位置轮播图adapter
 *
 * @auth lipf on 2015/1/29.
 */
public class BannerPagerAdapter extends PagerAdapter {

    private List<BannerNode> mBannerNodeList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private ImageLoader mImageLoader = HttpUtils.getImageLoader();
    private OnClickBannerListener mSelfOnClickBannerListener;

    public BannerPagerAdapter(Context context, OnClickBannerListener onClickBannerListener) {
        mSelfOnClickBannerListener = onClickBannerListener;
        this.mBannerNodeList = new ArrayList<BannerNode>();
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    /**
     * 替换banner数据源
     * @param bannerNodeList
     */
    public void replaceBanner(List<BannerNode> bannerNodeList) {
        synchronized (mBannerNodeList) {
            mBannerNodeList.clear();
            mBannerNodeList.addAll(bannerNodeList);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return mBannerNodeList.size() > 0 ? Integer.MAX_VALUE : 0 ;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final BannerNode node = mBannerNodeList.get(position % mBannerNodeList.size());
        View view = mLayoutInflater.inflate(R.layout.view_card_banner_item, container, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelfOnClickBannerListener != null) {
                    mSelfOnClickBannerListener.onClickBanner(node);
                }
            }
        });
        NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.banner_item_image);
        final TextView descTextView = (TextView) view.findViewById(R.id.banner_item_desc);
        final TextView titleTextView = (TextView) view.findViewById(R.id.banner_item_title);
        if (TextUtils.isEmpty(node.description)) {
            descTextView.setVisibility(View.GONE);
        } else {
            descTextView.setVisibility(View.VISIBLE);
            descTextView.setText(node.description);
        }
        if (TextUtils.isEmpty(node.title)) {
            titleTextView.setVisibility(View.GONE);
        } else {
            titleTextView.setVisibility(View.VISIBLE);
            titleTextView.setText(node.title);
            titleTextView.setBackgroundDrawable(Utils.getShapeDrawable(mContext.getResources().getColor(R.color.subject_button_bg), ScreenUtils.dp2px(2)));
        }
        imageView.setErrorImageResId(R.drawable.image_error_background);
        imageView.setDefaultImageResId(R.drawable.image_error_background_gray);

        imageView.setImageUrl(node.image, mImageLoader);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (View) object;
    }

    public interface OnClickBannerListener {
        public void onClickBanner(BannerNode node);
    }
}
